/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/7/31 11:17
 */
// 有序数组中的单一元素
    // https://leetcode.cn/problems/single-element-in-a-sorted-array/
    // 二分查找
    // 思路: 取中间, 算l和mid间距离的奇偶性 && mid和r间的奇偶性, 哪个是奇数就在哪边
public class Solution540 {
    public int singleNonDuplicate(int[] nums) {
        int l = 0, r = nums.length - 1,m;
        while (l < r) {
            m = l + (l + r) / 2;
            if (m % 2 == 1) {
                m--;
            }
            if (nums[m] == nums[m + 1]) {
                l = m + 2;
            } else {
                r = m;
            }
        }
        return nums[l];
    }
}
